package com.sharelords.icp.web.result;

import com.sharelords.icp.web.constant.ReturnCodeEnum;

/**
 * 视图结果返回
 *
 * @author 夜舞旋
 */
public class ViewResult<T> {

    private int returnCode;
    private String returnMsg;
    private T result;

    public int getReturnCode() {
        return returnCode;
    }

    public String getReturnMsg() {
        return returnMsg;
    }

    public T getResult() {
        return result;
    }

    /**
     * 成功
     *
     * @param t
     * @return
     */
    public ViewResult<T> success(T t) {
        this.returnCode = ReturnCodeEnum.SUCCESS.getReturnCode();
        this.returnMsg = ReturnCodeEnum.SUCCESS.getReturnMsg();
        this.result = t;
        return this;
    }

    /**
     * 失败
     *
     * @param returnCodeEnum
     * @return
     */
    public ViewResult<T> failed(ReturnCodeEnum returnCodeEnum) {
        return this.failed(returnCodeEnum, null);
    }

    /**
     * 失败
     *
     * @param returnCodeEnum
     * @param t
     * @return
     */
    public ViewResult<T> failed(ReturnCodeEnum returnCodeEnum, T t) {
        if (returnCodeEnum == null) {
            returnCodeEnum = ReturnCodeEnum.FAILED;
        }
        this.returnCode = returnCodeEnum.getReturnCode();
        this.returnMsg = returnCodeEnum.getReturnMsg();
        this.result = t;
        return this;
    }

}
